home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11.lha / ccs-lib / tools / convert / toicc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-15  |  7.0 KB  |  235 lines

  1. /*
  2. %    TOICC . C
  3. %
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5.  
  6. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  7. Permission is granted to reproduce this software for non-commercial
  8. purposes provided that this notice is left intact.
  9.  
  10. It is acknowledged that the U.S. Government has rights to this software
  11. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  12. and the University of California.
  13.  
  14. This software is provided as a professional and academic contribution
  15. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  16. with no warranties of any kind whatsoever, no support, no promise of
  17. updates, or printed documentation. By using this software, you
  18. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  19. University of California shall have no liability with respect to the
  20. infringement of other copyrights by any part of this software.
  21.  
  22. For further information about this notice, contact William Johnston,
  23. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  24. (wejohnston@lbl.gov)
  25.  
  26. For further information about this software, contact:
  27.     Jin Guojun
  28.     Bld. 50B, Rm. 2275,
  29.     Lawrence Berkeley Laboratory, Berkeley, CA, 94720
  30.     g_jin@lbl.gov
  31. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  32. % Read any images (man ccs) and output a ICC image padded to 8-pixel width.
  33. %
  34. % AUTHOR:    Jin Guojun, LBL - 1991
  35. */
  36.  
  37. #include "header.def"
  38. #include "imagedef.h"
  39. #include <time.h>
  40.  
  41. #ifndef    ICC_HEADER_LEN
  42. #define    ICC_HEADER_LEN    60
  43. #endif
  44.  
  45. arg_fmt_list_string    arg_list[] =    {
  46.     {"-k", "%b", True, 1, 0, "    keep going & ignore error"},
  47.     {"-f", "%b", True, 1, 1, "    enhance on forground"},
  48.     {"-g", "%f", 0., 1, 1, "    gamma for background"},
  49.     {"-l", "%-", 0, 1, 0, "    rotate left"},
  50.     {"-r", "%+", 0, 1, 0, "    rotate right"},
  51.     {"-t", "%s", No, 1, 1, "    title"},
  52.     {"-w", "%b", True, 1, 0, "    without header output"},
  53. {"I/O:    [<] input [[ > | ] output]", "0", 0, 0, 0, "End"},    NULL};
  54.  
  55. ICC_HEADER    icc_hd = {ICC_MAGIC, ICC_HEADER_LEN};
  56. U_IMAGE    uimg;
  57. bool    Etype, nostop, rot, wh;
  58. float    gamma;
  59. int    *hist;
  60. char    *obuf[4];
  61.  
  62. #define    rows    uimg.height
  63. #define    cols    uimg.width
  64.  
  65. #ifndef    GAMMA_BASE
  66. #define    GAMMA_BASE    2.49998
  67. #endif
  68.  
  69. main(ac, av)
  70. int    ac;
  71. char*    av[];
  72. {
  73. char    **fl, *title, date_time[8];
  74. int    nf, maxval, new_rows, new_cols, r_size;
  75. long    clock;
  76. MType    x_size, row, cnt;
  77. struct    tm*    date;
  78. pixel**    xel24;
  79.  
  80.     uimg.color_dpy = True;
  81.     format_init(&uimg, IMAGE_INIT_TYPE, COLOR_PS, ICC, *av, "D20-1");
  82.  
  83.     if ((nf=parse_argus(&fl, ac, av, arg_list,
  84.         &nostop, &Etype, &gamma, &rot, &rot, &title, &wh)) < 0)
  85.         exit(nf);
  86.     if (gamma)    {
  87.         if (Etype)
  88.             gamma = -gamma;
  89.         gamma -= GAMMA_BASE;
  90.     }
  91.     if (nf && !freopen(uimg.name=fl[0], "rb", stdin))
  92.         syserr("input file -- %s", fl[0]);
  93.  
  94. io_test(fileno(in_fp), {parse_usage(arg_list); exit(-1);});
  95.  
  96. if ((*uimg.header_handle)(HEADER_READ, &uimg, 0, 0, True) < 0)
  97.     syserr("unknown image type");
  98.  
  99. if (uimg.in_type==PPM || uimg.in_type==PNM)
  100.     xel24 = (pixel**)(*uimg.std_swif)(FI_PNM_MAXVAL, &uimg, &maxval);
  101. (*uimg.std_swif)(FI_LOAD_FILE, &uimg, nostop ? NULL : uimg.name,
  102.     True /* don't change format, and save PNM loading buffer */);
  103.  
  104. clock = time((long *)0);
  105. date  = localtime(&clock);
  106.  
  107. if (rot)
  108.     new_rows = cols,    new_cols = rows;
  109. else    new_rows = rows,    new_cols = cols;
  110.  
  111. r_size = x_size = (new_cols + 7) & 0xFFFFFFF8L;
  112.     /* creation date */
  113.     icc_hd.date = (((long)date->tm_year << 24) & 0xFF000000) +
  114.         (((long)(date->tm_mon + 1) << 16) & 0x00FF0000) +
  115.         (((long)date->tm_mday << 8)    & 0x0000FF00) +
  116.         ((long)date->tm_hour        & 0x000000FF);
  117.     icc_hd.time = (((long)date->tm_min << 24) & 0xFF000000) +
  118.         (((long)date->tm_sec << 16) & 0x00FF0000);
  119.     if (uimg.name)
  120.         strncpy(icc_hd.user_name, uimg.name, 32);
  121.     /* subfile header length */
  122.     icc_hd.img_hd.len = sizeof(icc_hd.img_hd);
  123.         /* subfile length */
  124.     icc_hd.img_hd.length = icc_hd.img_hd.len + (x_size * new_rows);
  125.     icc_hd.img_hd.File_Type = 7;    /* ICC type */
  126.     icc_hd.img_hd.orig_width = new_cols;
  127.     icc_hd.img_hd.x_size = x_size;    /* image new width */
  128.     icc_hd.img_hd.y_size = icc_hd.img_hd.orig_height = new_rows;
  129.     icc_hd.img_hd.gray_scale =
  130.         uimg.color_form==CFM_SGF;    /* color=0, greyscale=1    */
  131.     icc_hd.img_hd.planes =
  132.         icc_hd.img_hd.gray_scale ? 1 : 3;/*    plane count    */
  133.  
  134.     if (!wh)
  135.         fwrite(&icc_hd, sizeof(icc_hd), 1, out_fp);
  136.     msg("image size:   %ld(w) x %ld(h)\n", x_size, new_rows);
  137.     if (rot)
  138.         r_size = cols;
  139.     for (cnt=0; cnt < icc_hd.img_hd.planes; cnt++)
  140.         obuf[cnt] = nzalloc(x_size, new_rows, "icc-obuf");
  141.  
  142.     if (uimg.color_form != CFM_SGF && uimg.color_form != CFM_SEPLANE) {
  143.     register char    *pr = obuf[0],    *pg = obuf[1],    *pb = obuf[2];
  144.  
  145.     switch (uimg.in_type) {
  146.     register int    col;
  147.     case PPM:
  148.     case PNM:
  149.         for (row=0; row < rows; row++)    {
  150.         register pixel    *pP = xel24[row];
  151.             for (col=0; col < cols; col++, pP++)
  152.             pr[col] = PPM_GETR(*pP),
  153.             pg[col] = PPM_GETG(*pP),
  154.             pb[col] = PPM_GETB(*pP);
  155.         pr+=r_size,    pg+=r_size,    pb+=r_size;
  156.         }
  157.         free(xel24);
  158.         break;
  159.     default:
  160.         if (uimg.channels==3)
  161.             if (uimg.color_form == CFM_ILL ||
  162.             uimg.in_type==TiFF && uimg.color_form != CFM_ILC) {
  163.             byte*    scan[3];
  164.                 scan[2] = (byte*)uimg.src - cols;
  165.                 for (row=0; row < rows; row++)    {
  166.                     scan[0] = scan[2] + cols;
  167.                     scan[1] = scan[0] + cols;
  168.                     scan[2] = scan[1] + cols;
  169.                     memcpy(pr, scan[0], cols);
  170.                     memcpy(pg, scan[1], cols);
  171.                     memcpy(pb, scan[2], cols);
  172.                     pr+=r_size, pg+=r_size, pb+=r_size;
  173.                 }
  174.             }
  175.             else {
  176.             register byte*    ibp = (byte*) uimg.src;
  177.             for (row=rows; row--;pr+=r_size, pg+=r_size, pb+=r_size)
  178.                 for (col=0; col < cols; col++)
  179.                 pr[col] = *ibp++,
  180.                 pg[col] = *ibp++,
  181.                 pb[col] = *ibp++;
  182.             }
  183.         else    {
  184.         register byte    *cmap[3], *bp = (byte *)uimg.src;
  185.             cmap[0] = reg_cmap[0];
  186.             cmap[1] = reg_cmap[1];
  187.             cmap[2] = reg_cmap[2];
  188.             if (cmap[0] == NULL)
  189.                 rlemap_to_regmap(cmap, &rle_dflt_hdr);
  190.             for (row=rows; row--; bp+=col, pr+=r_size, pg+=r_size, pb+=r_size)
  191.                 for (col=0; col < cols; col++)
  192.                 pr[col] = cmap[0][bp[col]],
  193.                 pg[col] = cmap[1][bp[col]],
  194.                 pb[col] = cmap[2][bp[col]];
  195.         }
  196.     }
  197.     }
  198.     else while (cnt--)    {
  199.     register char    *po = obuf[cnt], *ps = (char*)uimg.src + cols*rows*cnt;
  200.     for (row=rows; row--; po+=r_size, ps+=cols)
  201.         memcpy(po, ps, cols);
  202.     }
  203.     free(uimg.src);
  204.  
  205.     if (gamma) {
  206.     LKT*    lkt = zalloc(MaxColors, sizeof(*lkt), "icc-lkt");
  207.     hist = nzalloc(MaxColors, sizeof(*hist), "icc-hist");
  208.     row = new_rows * x_size;
  209.     for (cnt=icc_hd.img_hd.planes; cnt--;)
  210.         apply_gamma(obuf[cnt], row, lkt);
  211.     }
  212.     if (rot) {
  213.     cnt = icc_hd.img_hd.planes;
  214.     obuf[cnt] = nzalloc(x_size, new_rows, "icc-rot");
  215.     while (cnt--)
  216.         rotate90(obuf[cnt], obuf[cnt+1], new_rows, x_size, 1, rot+1);
  217.     }
  218.     for (cnt=0, rot=rot!=0; cnt<icc_hd.img_hd.planes; cnt++)
  219.     if (fwrite(obuf[cnt + rot], new_rows, x_size, out_fp) != x_size)
  220.         syserr("write data");
  221.     for (cnt = icc_hd.img_hd.planes + rot; cnt--;)
  222.     free(obuf[cnt]);
  223. exit(0);
  224. }
  225.  
  226. apply_gamma(bp, size, lkt)
  227. register byte*    bp;
  228. register LKT*    lkt;
  229. {
  230. register maxval = histogram_calc(bp, size, hist);
  231.     eta_curve(lkt, gamma, maxval, Etype);
  232.     for (maxval=size; maxval--;)
  233.         bp[maxval] = lkt[bp[maxval]];
  234. }
  235.